<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height: 60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addRole"
            >新增角色</el-button
          >
        </el-row>
        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column
            type="index"
            :index="indexMethod"
            label="序号"
            width="120"
          />
          <el-table-column prop="name" label="角色名称" width="240" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作" width="240" fixed="right">
            <template #default="{ row }">
              <el-button size="small" @click="assginPermission(row.id)"
                >分配权限</el-button
              >
              <el-button size="small" @click="editRole(row.id)">编辑</el-button>
              <el-button size="small" type="danger">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <!-- 分页组件 -->
          <el-pagination
            :total="total"
            :page-size="params.pagesize"
            layout="prev,pager,next"
            @current-change="currentChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 新增 -->
    <add-role
      ref="addRole"
      :show-dialog="showDialog"
      @close-dialog="closeDialog"
      @update-list="fetchList"
    />
    <AssignRole
      :cur-id="curId"
      :show-assign-dialog="showAssignDialog"
      @close-dialog="closeAssignDialog"
    />
  </div>
</template>

<script>
import { getRoleList } from '@/api/setting'
import AddRole from './components/add-role.vue'
import AssignRole from './components/assign-role.vue'
export default {
  components: {
    AddRole,
    AssignRole
  },
  data () {
    return {
      list: [],
      params: {
        page: 1,
        pagesize: 5
      },
      total: 0,
      showDialog: false,
      showAssignDialog: false,
      curId: '' // 记录当前要给谁添加的id
    }
  },
  mounted () {
    this.fetchList()
  },
  methods: {
    async fetchList () {
      const res = await getRoleList(this.params)
      // console.log(res.data.rows)
      this.list = res.data.rows
      this.total = res.data.total
    },
    currentChange (page) {
      // console.log(page)
      this.params.page = page // 先改page为当前点击的页数
      this.fetchList() // 重新发送接口请求 [params.page已经是最新的页数了]
    },
    addRole () {
      this.showDialog = true
    },
    closeDialog () {
      this.showDialog = false
    },
    editRole (id) {
      this.showDialog = true
      this.$refs.addRole.fetchDetail(id)
    },
    assginPermission (id) {
      this.curId = id
      this.showAssignDialog = true
    },
    closeAssignDialog () {
      this.showAssignDialog = false
    },
    indexMethod (index) {
      // return index * 2
      return index + (this.params.page - 1) * this.params.page + 1
    }
  }
}
</script>

<style lang="scss" scoped></style>
